

////////////////////////////////////////////////////////////////////////////////
// this script 
//   - get the size of jar file
//   - add the line "MIDlet-Jar-Size: size" to the jad file
//
// syntax: 
//   java -jar BeanShell.jar jarSize.Jscript jarFile jadFile
//      jarFile : path+name of the jar file
//      jadFile : path+name of the jad file
////////////////////////////////////////////////////////////////////////////////
import java.util.*;
import java.io.*;
import java.lang.*;

class jarSize
{

    // --- verbose
	boolean verbose = false;
	boolean bigEndian = false;

            
    // --- LOG
    void LOG(String a)
    {
        System.out.println(a);
    }
            
    // --- ERR
    void ERR(String a)
    {
    	System.err.println(a);
    }
			
    // --- ASSERT
	void ASSERT(boolean cond, String a) throws Exception
    {
        if (cond) 
        {
            ERR(a);
            throw new Exception(a);
        }
    }

    // --- write32BigEndian
	void write32BigEndian(FileOutputStream out, int value) throws Exception
    {
		byte [] b = new byte[4];
		b[0] = (byte)((value >> 24) & 0xFF);
		b[1] = (byte)((value >> 16) & 0xFF);
		b[2] = (byte)((value >> 8) & 0xFF);
		b[3] = (byte)(value & 0xFF);
		out.write(b, 0, 4);
	}
    
    // --- write16BigEndian
	void write16BigEndian(FileOutputStream out, int value) throws Exception
	{
		byte [] b = new byte[2];
		b[0] = (byte)((value >> 8) & 0xFF);
		b[1] = (byte)(value & 0xFF);
		out.write(b, 0, 2);
	}
			
	// --- write32LittleEndian
	void write32LittleEndian(FileOutputStream out, int value) throws Exception
	{
		byte [] b = new byte[4];
		b[0] = (byte)(value & 0xFF);
		b[1] = (byte)((value >> 8) & 0xFF);
		b[2] = (byte)((value >> 16) & 0xFF);
		b[3] = (byte)((value >> 24) & 0xFF);
		out.write(b, 0, 4);
	}
			
    // --- write16LittleEndian
	void write16LittleEndian(FileOutputStream out, int value) throws Exception
	{
		byte [] b = new byte[2];
		b[0] = (byte)(value & 0xFF);
		b[1] = (byte)((value >> 8) & 0xFF);
		out.write(b, 0, 2);
	}
	
	// --- write32
	void write32(FileOutputStream out, int value) throws Exception
	{
        if (bigEndian)
            write32BigEndian(out, value);
		else
			write32LittleEndian(out, value);
	}
			
	// --- write16
	void write16(FileOutputStream out, int value) throws Exception
	{
		if (bigEndian)
            write16BigEndian(out, value);
		else
				write16LittleEndian(out, value);
	}




















long size = -1;

// append MIDlet-Jar-Size line to a file
void proceed(String filename) throws Exception
{
	try {
		FileWriter file = new FileWriter(filename, true);
		String str = "MIDlet-Jar-Size: "+size+"\r\n";
		file.write(str);
		file.close();
	} catch (Exception e) {
		ERR("cannot open/modify file:"+filename);
		System.exit(-1);
	}
}


// get size of the file
boolean getSize(String filename) throws Exception
{
	File file = new File(filename);
	if (! file.exists() )
		return false;

	size = file.length();
	return true;
}

	void exec(String[] args) throws Exception
	{
		if ((args.length == 2) && (getSize(args[0]))) {
			proceed(args[1]);
		
		} else {
			ERR(args[0]+" invalid argument");
			System.exit(-1);
		}
	}
	
	static public void main(String[] args)
	{
	    try {
	        jarSize m = new jarSize();
	        m.exec(args);
	    } catch(Exception e)
	    { }
	}
	
}
